package main

import (
	"fmt"
	"strconv"
	"time"

	"github.com/xuri/excelize/v2"
)

func main() {
	f, err := excelize.OpenFile("C:\\Users\\ping\\Documents\\WeChat Files\\wxid_07gkstlh2bad22\\FileStorage\\File\\2021-10\\0824.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}

	var result []*Info

	// Get value from cell by given worksheet name and axis.
	_, err = f.GetCellValue("Sheet1", "B2")
	if err != nil {
		fmt.Println("0001", err)
		return
	}
	//fmt.Println(cell)
	// Get all the rows in the Sheet1.
	rows, err := f.GetRows("Sheet1")
	if err != nil {
		fmt.Println("111", err)
		return
	}

	// style, err := f.NewStyle(`{"number_format": 0}`)
	// if err != nil {
	// 	panic(err)
	// }
	// f.SetColStyle("Sheet1", "C", style)

	for rowIndex, row := range rows {
		fmt.Println("rowIndex:", rowIndex, row)
		info := &Info{}
		for i, colCell := range row {
			switch i {
			case 0:
				info.Entname = colCell
			case 1:
				info.XyCode = colCell
			case 2:
				//				info.EsDate = excelDateToDate(colCell, rowIndex)
				info.EsDate = colCell
			case 3:
				info.EsYear = colCell
			case 4:
				info.RegCap = colCell
				info.RegRange = genRange(colCell, rowIndex)
			case 5:
				info.Address = colCell
			case 6:
				info.Industry = colCell
			}
		}
		result = append(result, info)
	}
	jsonWrite(result)
	// Close the spreadsheet.
	//if err = f.Close(); err != nil {
	//fmt.Println(err)
	//}
}

func genRange(s string, i int) string {

	if i == 0 {
		return ""
	}

	floatA, err := strconv.ParseFloat(s, 64)
	if err != nil {
		panic(err)
	}

	if floatA < 100 {
		return "100万元以下"
	} else if floatA < 500 {
		return "100(含)-500万元"
	} else if floatA < 1000 {
		return "500(含)-1000万元"
	} else if floatA < 5000 {
		return "1000(含)-5000万元"
	}

	return "1亿元以上"
}

func excelDateToDate(excelDate string, i int) string {
	if i == 0 {
		return ""
	}
	fmt.Println("excelDate:" + excelDate)

	excelTime := time.Date(1899, time.December, 30, 0, 0, 0, 0, time.UTC)
	var days, _ = strconv.Atoi(excelDate)
	t := excelTime.Add(time.Second * time.Duration(days*86400))

	return t.Format("2006-01-02")

}
